package org.csu.pet_store_2.Service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.csu.pet_store_2.Common.CommonResponse;
import org.csu.pet_store_2.Domain.Cart;
import org.csu.pet_store_2.Service.CartService;
import org.csu.pet_store_2.persistence.CartMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

@Service
public class CartServiceImpl implements CartService {

    @Autowired
    private CartMapper cartMapper;
    public String additemtocart(String userId,String itemId,Integer quantity)
    {
        Cart cart=new Cart();
        cart.setItemId(itemId);
        cart.setQuantity(quantity);
        cart.setUserId(userId);
        cartMapper.insert(cart);
        return "添加成功";
    }
    public String deletefromcart(String userId,String itemId)
    {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("userId",userId);
        queryWrapper.eq("itemId",itemId);
        cartMapper.delete(queryWrapper);
        return "删除成功";
    }
    public ArrayList<Cart> getCartByUserId(String userId)
    {
        ArrayList<Cart> cartArrayList=new ArrayList<>();
        List<Cart> cartList=cartMapper.getCartItemByUserid(userId);
        cartArrayList.addAll(cartList);
        return cartArrayList;
    }
    public String updatequantity(String userId,String itemId,Integer quantity){
        Cart cart=new Cart();
        cart.setUserId(userId);
        cart.setItemId(itemId);
        cart.setQuantity(quantity);
        QueryWrapper queryWrapper1=new QueryWrapper();
        queryWrapper1.eq("userId",userId);
        queryWrapper1.eq("itemId",itemId);
        cartMapper.update(cart,queryWrapper1);
        return "更新成功";
    }
    public String cleancart(@RequestParam("id") String userid)
    {
        QueryWrapper queryWrapper =new QueryWrapper();
        queryWrapper.eq("userid",userid);
        cartMapper.delete(queryWrapper);
        return "清空成功";
    }
}
